﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;

/// <summary>
/// Summary description for QLTinTuc
/// </summary>
public class QLTinTuc
{
    DBConnection db = new DBConnection ();
	public QLTinTuc()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public DataTable Load_LoaiTinTuc()
    {
        string sql = "select MaLoai,TenLoai from LoaiTT";
        return db.Get_data(sql);
    }
    public void Insert_LoaiTinTuc(string tenloai)
    {
        string sql = "insert into LoaiTT(tenloai) values(@tenloai)";
        SqlParameter p1 = new SqlParameter("@tenloai", tenloai);
        db.thuchienlenh(sql, p1);
    }
    public void Delete_LoaiTT(string maloai)
    {
        string sql = "delete from Loaitt where maloai=@maloai";
        SqlParameter p1 = new SqlParameter("@maloai", maloai);
        db.thuchienlenh(sql,p1);
    }
    public void Update_LoaiTT(string maloai,string tenloai)
    {
        string sql = "update loaitt set tenloai=@tenloai where maloai=@maloai";
        SqlParameter p1 = new SqlParameter("@maloai", maloai);
        SqlParameter p2 = new SqlParameter("@tenloai",tenloai);
        db.thuchienlenh(sql, p1, p2);
    }

    public DataTable Load_TinTuc()
    {
        string sql = "select loaitt.maloai, matt,tentt,ngaydang,tenloai,noidung from tintuc,loaitt where tintuc.maloai=loaitt.maloai";
        return db.Get_data(sql);
    }
    public DataTable Load_CT_TinTuc(string matt)
    {
        string sql = "select tentt,ngaydang,tenloai,noidung from tintuc,loaitt where tintuc.maloai=loaitt.maloai and matt=@matt";
        SqlParameter p1 = new SqlParameter("@matt",matt);
        return db.Get_data(sql, p1);
        return db.Get_data(sql);
    }
    public string Load_NoiDungTT(string matt)
    {
        string sql = "select noidung from tintuc where matt=@matt";
        SqlParameter p1 = new SqlParameter("@matt",matt);
        object kq = db.LayDuLieu(sql, p1);
        return kq.ToString();
    }
    public void Insert_TinTuc(string tentt,string noidung,DateTime ngaydang,string maloai)
    {
        string sql = "insert into tintuc(tentt,noidung,ngaydang,maloai) values(@tentt,@noidung,@ngaydang,@maloai)";
        SqlParameter p1 = new SqlParameter("@tentt",tentt);
        SqlParameter p2 = new SqlParameter("@noidung",noidung);
        SqlParameter p3 = new SqlParameter("@ngaydang",ngaydang);
        SqlParameter p4 = new SqlParameter("@maloai", maloai);
        db.thuchienlenh(sql, p1, p2, p3, p4);
    }
    public DataTable Update_TinTuc(string matt,string tentt, string noidung, string ngaydang, string maloai)
    {
        string sql = "update tintuc set tentt=@tentt,noidung=@noidung,ngaydang=@ngaydang,maloai=@maloai where matt=@matt";
        SqlParameter p1 = new SqlParameter("@matt", matt);
        SqlParameter p2 = new SqlParameter("@tentt", tentt);
        SqlParameter p3 = new SqlParameter("@noidung", noidung);
        SqlParameter p4 = new SqlParameter("@ngaydang", ngaydang);
        SqlParameter p5 = new SqlParameter("@maloai", maloai);
        return db.Get_data(sql, p1, p2, p3, p4, p5);
    }
    public DataTable Delete_TinTuc(string matt)
    {
        string sql = "delete from tintuc where matt=@matt";
        SqlParameter p1 = new SqlParameter("@matt", matt);
        return db.Get_data(sql, p1);
    }
}
